package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.entity.FybxEntity;
import io.renren.service.FybxService;
import io.renren.utils.PageUtils;
import io.renren.utils.QueryAuthority;
import io.renren.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 费用报销
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-30 12:06:14
 */
@RestController
@RequestMapping("fybx")
public class FybxController extends AbstractController{
	@Autowired
	private FybxService fybxService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("fybx:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        QueryAuthority query = new QueryAuthority(params);

		List<FybxEntity> fybxList = fybxService.queryList(query);
		int total = fybxService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(fybxList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{bxId}")
	@RequiresPermissions("fybx:info")
	public R info(@PathVariable("bxId") Integer bxId){
		Map map = fybxService.queryObject(bxId);
		
		return R.ok().put("map", map);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("fybx:save")
	public R save(@RequestBody JSONObject params){
		fybxService.save(params);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("fybx:update")
	public R update(@RequestBody JSONObject params){
		fybxService.update(params);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("fybx:delete")
	public R delete(@RequestBody Integer[] bxIds){
		fybxService.deleteBatch(bxIds);
		
		return R.ok();
	}


	/**
	 * 提交
	 */
	@RequestMapping("/submit")
	@RequiresPermissions("fybx:submit")
	public R submit(@RequestBody Integer id){
		String msg=fybxService.submit(id);
		if(StringUtils.isNotBlank(msg)){
			return R.error(1, msg);
		}
		return R.ok();
	}


	/**
	 * 批准
	 */
	@RequestMapping("/process")
	public R process(@RequestBody FybxEntity entity){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(entity.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");
		}

		fybxService.process(entity);

		return R.ok();
	}

	/**
	 * 拒绝
	 */
	@RequestMapping("/reject")
	public R reject(@RequestBody FybxEntity entity){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(entity.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");

		}
		fybxService.reject(entity);

		return R.ok();
	}

	
}
